package apphttp

import (
	"net/http"
	"TanguyWeb/main/utils/sms"
	"TanguyWeb/main/bean"
	"TanguyWeb/main/session"
	"TanguyWeb/main/config"

	"time"
	"gitee.com/DengAnbang/goutils/loge"
)

/**
 发送验证码
 phone
 sessionID(可以nil)
 */
func sendSms(w http.ResponseWriter, r *http.Request) error {
	loge.ENullable(r.ParseForm())
	checkSession, err := checkSession(r)
	checkErr(err)
	if checkSession == nil {
		checkSession = session.NewSession()
	}

	values := r.Form
	phone := values.Get("phone")
	checkEmpty(phone, "手机号不能为空!")
	//code, err := sms.SendSms(phone)
	code, err := sms.SendSmsDeBug(phone)
	checkErr(err)
	checkSession.SetValue("captcha", code)
	checkSession.SetValue("captchaTime", time.Now())
	checkSession.SetValue("phone", phone)
	return bean.NewResultData(checkSession.SessionID).SetDebugMeg("code:" + code)
}

func test(w http.ResponseWriter, r *http.Request) error {
	r.ParseForm()
	w.Write([]byte(r.Form.Get("test")))
	return nil
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) error {
	//if r.URL.Path == "/" {
	//	http.Redirect(w, r, "/app/html/test", http.StatusFound)
	//}
	//s := r.URL.Path
	//if s == "/app/file" {
	//	server := http.FileServer(http.Dir(config.FileRootPath))
	//	server.ServeHTTP(w, r)
	//	return nil
	//}
	//http.FileServer(http.Dir(fileutils.FileRootPath))
	//w.WriteHeader(http.StatusNotFound)
	return bean.NewResultError(config.Err, "404:").SetDebugMeg("错误的Path:" + r.URL.Path)

}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func checkEmpty(s string, errMsg string) {
	if len(s) <= 0 {
		panic(bean.NewResultError(config.Err, errMsg))
	}
}
